﻿Imports System.Data.SqlClient

Public Class frmExchangeRatesRegistry

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Try
            If Me.txtCurrency.Text.Trim.Length = 0 OrElse Me.txtRate.Text.Trim.Length = 0 Then
                Exit Sub
            Else
                Me.Cursor = Cursors.WaitCursor

                Dim cmd As New SqlCommand("Insert Into ExchangeRates (Currency,Rate) Values (@Currency,@Rate)", cnn)

                cnn.Open()

                'Add values
                cmd.Parameters.AddWithValue("@Currency", Me.txtCurrency.Text.Trim)
                cmd.Parameters.AddWithValue("@Rate", Me.txtRate.Text.Trim)

                cmd.ExecuteNonQuery()
                cnn.Close()

                FillExchangeRates()

                Me.txtCurrency.Clear()
                Me.txtRate.Clear()
                Me.txtCurrency.Focus()

                Me.Cursor = Cursors.Default
            End If
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub GridCountryCodes_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridCountryCodes.CellClick
        If e.ColumnIndex = Me.DGColUpdate.Index Then
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim cmd As New SqlCommand
                Dim Trans As SqlTransaction

                cnn.Open()
                Trans = cnn.BeginTransaction
                cmd.Connection = cnn
                cmd.Transaction = Trans

                'Save Contacts
                For Each row As DataGridViewRow In Me.GridCountryCodes.Rows
                    cmd.Parameters.Clear()
                    cmd.CommandText = "Update ExchangeRates Set Currency=@Currency,Rate=@Rate Where SNo=" & row.Cells(0).Value

                    cmd.Parameters.AddWithValue("@Currency", row.Cells(1).Value)
                    cmd.Parameters.AddWithValue("@Rate", row.Cells(2).Value)
                    cmd.ExecuteNonQuery()
                Next

                Trans.Commit()
                cnn.Close()

                MsgBox("Updated Successfully!", MsgBoxStyle.Information, Me.Text)

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try

        ElseIf e.ColumnIndex = Me.DGColDelete.Index Then
            If MsgBox("Confirm delete?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                Try
                    Me.Cursor = Cursors.WaitCursor

                    Dim cmd As New SqlCommand("Delete From ExchangeRates Where SNo=" & Me.GridCountryCodes.CurrentRow.Cells(0).Value, cnn)

                    cnn.Open()
                    cmd.ExecuteNonQuery()
                    cnn.Close()

                    FillExchangeRates()

                    Me.Cursor = Cursors.Default
                Catch ex As Exception
                    Me.Cursor = Cursors.Default
                    If cnn.State = ConnectionState.Open Then
                        cnn.Close()
                    End If
                    MsgBox(ex.ToString)
                End Try
            End If
        End If
    End Sub

    Sub FillExchangeRates()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From ExchangeRates Order By Currency", cnn)
            Dim Reader As SqlDataReader

            Me.GridCountryCodes.Rows.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Dim Item As New ListBox
                Me.GridCountryCodes.Rows.Add(New String() {Reader.Item(0), Reader.Item(1), Reader.Item(2), "Update", "Delete"})
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub frmExchangeRatesRegistry_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        FillExchangeRates()
    End Sub
End Class